// pages/property/wyfList/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wyfList:[],
    isLoading:true,
    room_id:0,
    hasData:false,
    room:{},
    checkIndexs:[],
    yearNum:'',
    totalPrice:0,
    totalPriceYuan:0,
    checkIds:[],
    activeNames:[],
    isShowPopup:false,
    beginMonth:'',
    endMonth:'',
    currentDate:new Date(2022, 0, 1),
    minDate: new Date(2022, 0, 1).getTime(),
    dateType:0,
    beginYear:'',
    chooseTotalCount:0,
    checkAll:false,
    feeType:'wyf', // 费用类型  wyf - 物业费  eletrict - 电费  water - 水费
    wyfWaterBand:0,
    tapTime:0,
    fromType:'page',//page - 小程序内页面跳转来的，qrcode - 扫码进来的
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    var that = this
    
    if(options.q){
      this.data.fromType = 'qrcode';
      var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
      q = "?" + q.split("?")[1];
      this.data.query = q;
      console.log('q',q)
      options = common_Js.getQueryVariable(q);

      if(options.id){
        this.data.room_id = parseInt(options.id);
      }
    }else{
      if(options.id){
        this.data.room_id = parseInt(options.id);
      }
      if(options.room_id){
        this.data.room_id = parseInt(options.room_id);
      }
    }

    if(options.type){
      this.data.feeType = options.type;
    }

    var date = new Date();
    var year = date.getFullYear();

    this.data.beginMonth = year +'-01';
    this.data.endMonth = year +'-12';

    
    this.setData({
      beginMonth: this.data.beginMonth,
      endMonth: this.data.endMonth,
    })
    
    var isLogin = app.getStorageSync("isLogin");
    if(isLogin == 0){
      wx.navigateTo({
        url: '/pages/login/index?from_type=fee&roomId=' + this.data.room_id,
      })
    }else{
    }

    // this.onHttpInit();
  },
  onShow(){
    console.log('this.data.room_id', this.data.room_id)
    if(this.data.room_id > 0){
      this.onHttpInit();
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/estate/fee/order/list', {
      room_id: that.data.room_id,
      begin_month: that.data.beginMonth,
      end_month: that.data.endMonth,
      fee_type: that.data.feeType,
      from_type: 'admin'
    }, function (res) {
      console.log("我绑定的房间列表", res)
      if (res.data.code == 0) {
        that.data.wyfList =  res.data.data.all_templates;
        var firstYear = res.data.data.firstYear;
        var endYear = res.data.data.endYear;
        that.data.beginYear = firstYear;
        that.data.beginMonth = firstYear +'-01';
        that.data.endYear = endYear;
        that.data.endMonth = endYear +'-12';
        that.data.wyfWaterBand = res.data.data.wyfWaterBand
        that.setData({
          hasData:true,
          wyfWaterBand: res.data.data.wyfWaterBand,
          beginMonth: that.data.beginMonth,
          endMonth: that.data.endMonth,
          room: res.data.data.room,
          wyfList: res.data.data.all_templates,
          beginYear: firstYear,
          endYear: endYear,
        })
        // that.initActiveName(res.data.data.all_templates)
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    });
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  handleClearChoose(e){
    console.log('handleClearChoose',e)
    if(this.data.wyfWaterBand == 0){
      this.clearChoose(e);
    }else{
      this.clearChooseBand(e);
    }
  },
  clearChoose(e){
    var that = this;
    var feeIndex = e.currentTarget.dataset.feeIndex;
    console.log('feeIndex',feeIndex);
    console.log('that.data.wyfList[feeIndex]',this.data.wyfList[feeIndex])
    var templates = this.data.wyfList[feeIndex];
    var feeType = templates.feeType;
    console.log('templates',templates)
    for(var index in templates.templates){
      if(that.data.wyfList[feeIndex].templates[index].isCheck == 1){
        if(feeType == 'wyf'){
          if(that.data.wyfList[feeIndex].templates[index].rubbish!=undefined){
            console.log('that.data.totalPriceYuan   2',that.data.totalPriceYuan)
          }
        } else if(feeType == 'water'){
          if(that.data.wyfList[feeIndex].templates[index].jiaya!=undefined){
            console.log('that.data.totalPriceYuan   3',that.data.totalPriceYuan)
          }
        }
        console.log('that.data.totalPriceYuan   4',that.data.totalPriceYuan)
        that.data.wyfList[feeIndex].templates[index].isCheck = 0;
      }
    }

    that.data.wyfList[feeIndex].chooseList = [];
    that.data.totalPriceYuan -= that.data.wyfList[feeIndex].chooseTotalMoney
    that.data.wyfList[feeIndex].chooseTotalMoney = 0;

    console.log('that.data.totalPriceYuan',that.data.totalPriceYuan);

    this.setData({
      wyfList: that.data.wyfList,
      totalPrice: that.data.totalPriceYuan * 100,
    })
  },
  
  clearChooseBand(e){
    var that = this;
    // var feeIndex = e.currentTarget.dataset.feeIndex;
    for(var feeIndex in this.data.wyfList){
      console.log('that.data.wyfList[feeIndex]',this.data.wyfList[feeIndex])
      var templates = this.data.wyfList[feeIndex];
      var feeType = templates.feeType;
      console.log('templates',templates)
      for(var index in templates.templates){
        if(that.data.wyfList[feeIndex].templates[index].isCheck == 1){
          if(feeType == 'wyf'){
            if(that.data.wyfList[feeIndex].templates[index].rubbish!=undefined){
              console.log('that.data.totalPriceYuan   2',that.data.totalPriceYuan)
            }
          } else if(feeType == 'water'){
            if(that.data.wyfList[feeIndex].templates[index].jiaya!=undefined){
              console.log('that.data.totalPriceYuan   3',that.data.totalPriceYuan)
            }
          }
          console.log('that.data.totalPriceYuan   4',that.data.totalPriceYuan)
          that.data.wyfList[feeIndex].templates[index].isCheck = 0;
        }
      }
  
      that.data.wyfList[feeIndex].chooseList = [];
      that.data.totalPriceYuan -= that.data.wyfList[feeIndex].chooseTotalMoney
      that.data.wyfList[feeIndex].chooseTotalMoney = 0;
  
      console.log('that.data.totalPriceYuan',that.data.totalPriceYuan);  
    }
    this.setData({
      wyfList: that.data.wyfList,
      totalPrice: that.data.totalPriceYuan * 100,
    })
  },
  handleChoose(e){
    console.log('handleChoose',e)
    if(this.data.wyfWaterBand == 0){
      this.chooseFee(e);
    } else if(this.data.wyfWaterBand == 1){
      this.chooseFeeBand(e);
    }else{
      this.chooseFeeBandAll(e);
    }
  },
  /**
   * 
   * @param { 选中当前选中的费用的月份之前的} e 
   */
  chooseFee(e){
    console.log('chooseFee',e)
    var that = this;
    var template = e.currentTarget.dataset.item;
    var feeIndex = e.currentTarget.dataset.feeIndex;
    var tempIndex = e.currentTarget.dataset.index;

    console.log('template',template);
    var feeType = template.feeType;
    var chooseFee = { 
      feeType:feeType,beginMonth:'',
      endMonth:template.yearNum + '-' + template.monthNum,
      chooseList:[]
    }
    console.log('chooseFee ===', chooseFee)

    var templates = that.data.wyfList[feeIndex].templates;
    that.data.wyfList[feeIndex].chooseTotalMoney = 0;
    for(var index in templates){
      var template = templates[index];
      template.isCheck = 0;
      that.data.wyfList[feeIndex].templates[index] = template;
    }
    templates = that.data.wyfList[feeIndex].templates;
    
    var chooseTotalMoney = 0;
    var firstIndex = 0;
    for(var index in templates){
      var template = templates[index];
      var tempYear = parseInt(template.yearNum);
      var tempMonth =  parseInt(template.monthNum);
      if(index <= tempIndex) {
        if(template.state == 0){
            if(firstIndex == 0){
              chooseFee.beginMonth = template.yearNum +'-' + template.monthNum;
            }
            chooseFee.endMonth = tempYear +'-' + tempMonth;

            template.isCheck = 1;
            chooseTotalMoney += template.totalPrice;
    
            if(feeType == 'wyf'){
              if(template.rubbish!=undefined){
                chooseTotalMoney += template.rubbish.totalPrice;
                if(template.rubbish.youhuiPrice > 0){
                  chooseTotalMoney -= template.rubbish.youhuiPrice;
                }
              }
            } else if(feeType == 'water'){
              if(template.jiaya != undefined){
                chooseTotalMoney += template.jiaya.totalPrice;
                if(template.jiaya.youhuiPrice > 0){
                  chooseTotalMoney -= template.jiaya.youhuiPrice;
                }
              }
            }
            
            if(template.youhuiPrice > 0){
              chooseTotalMoney -= template.youhuiPrice;
            }

            chooseFee.chooseList.push(template.id);
            firstIndex++;
        }
      }
      that.data.wyfList[feeIndex].templates[index] = template;
    }
    // chooseFee.endMonth = template.yearNum +'-' + template.monthNum;
    console.log('chooseFee',chooseFee);

    that.data.wyfList[feeIndex].chooseList = chooseFee;
    that.data.wyfList[feeIndex].chooseTotalMoney = chooseTotalMoney;
    console.log('that.data.wyfList',that.data.wyfList);

    var totalPrice = 0;
    for(var index in that.data.wyfList){
        totalPrice += that.data.wyfList[index].chooseTotalMoney;
    }
    that.data.totalPriceYuan = totalPrice;
    console.log('totalPriceYuan',that.data.totalPriceYuan);
    that.setData({
      wyfList: that.data.wyfList,
      totalPrice: totalPrice * 100,
    })
  },
  /**
   * 
   * @param { 绑定选定月份 和之前的所有费用} e 
   */
  chooseFeeBand(e){
    console.log('chooseFeeBand',e)
    var that = this;
    var template = e.currentTarget.dataset.item;
    //点击的是哪个费用类型
    var feeIndex = e.currentTarget.dataset.feeIndex;
    //第几项  哪个月份的
    var tempIndex = e.currentTarget.dataset.index;
    var yearNum =  parseInt(that.data.wyfList[feeIndex].templates[tempIndex].yearNum);
    var monthNum =  parseInt(that.data.wyfList[feeIndex].templates[tempIndex].monthNum);

    // console.log('chooseFeeBand template',template);    
    // console.log('chooseFeeBand yearNum',yearNum);
    // console.log('chooseFeeBand monthNum',monthNum);

    console.log('this.data.wyfList',this.data.wyfList);
    
    // 遍历所有的费用类型
    for(var wyfIndex in this.data.wyfList){
      var templates = this.data.wyfList[wyfIndex].templates;
      var feeType = this.data.wyfList[wyfIndex].feeType;
      var chooseFee = { 
        feeType:feeType,
        beginMonth:'',
        endMonth: yearNum + '-' + monthNum,
        chooseList:[]
      }
      // console.log('chooseFeeBand ===', chooseFee)
      that.data.wyfList[wyfIndex].chooseTotalMoney = 0;
  
      // 初始化处理 去掉选中状态
      for(var index in templates){
        var template = templates[index];
        template.isCheck = 0;
        that.data.wyfList[wyfIndex].templates[index] = template;
      }
      templates = that.data.wyfList[wyfIndex].templates;
      // console.log('templates',templates)
      
      //判断当前选中的月份，选中当前月份之前的所有未交费的类型
      var chooseTotalMoney = 0;
      var firstIndex = 0;
      for(var index in templates){
        var template = templates[index];
        var tempYear = parseInt(template.yearNum);
        var tempMonth =  parseInt(template.monthNum);
        console.log('tempYear = ',tempYear)
        console.log('yearNum = ',yearNum)
        //&& tempMonth <= monthNum
        if((tempYear < yearNum) || (tempYear == yearNum && tempMonth <= monthNum)) {
          console.log('tempYear--yearNum',tempYear,yearNum)
          console.log('tempMonth--monthNum',tempMonth,monthNum)
          if(template.state == 0){
              if(firstIndex == 0){
                chooseFee.beginMonth = template.yearNum +'-' + template.monthNum;
              }
              chooseFee.endMonth = tempYear +'-' + tempMonth;
  
              template.isCheck = 1;
              chooseTotalMoney += template.totalPrice;
      
              if(feeType == 'wyf'){
                if(template.rubbish!=undefined){
                  chooseTotalMoney += template.rubbish.totalPrice;
                  if(template.rubbish.youhuiPrice > 0){
                    chooseTotalMoney -= template.rubbish.youhuiPrice;
                  }
                }
              } else if(feeType == 'water'){
                if(template.jiaya != undefined){
                  chooseTotalMoney += template.jiaya.totalPrice;
                  if(template.jiaya.youhuiPrice > 0){
                    chooseTotalMoney -= template.jiaya.youhuiPrice;
                  }
                }
              }
              
              if(template.youhuiPrice > 0){
                chooseTotalMoney -= template.youhuiPrice;
              }
  
              chooseFee.chooseList.push(template.id);
              firstIndex++;
          }
        }
        that.data.wyfList[wyfIndex].templates[index] = template;
      }  
      that.data.wyfList[wyfIndex].chooseList = chooseFee;
      that.data.wyfList[wyfIndex].chooseTotalMoney = chooseTotalMoney;
    }

    // console.log('that.data.wyfList',that.data.wyfList);

    var totalPrice = 0;
    for(var index in that.data.wyfList){
        totalPrice += that.data.wyfList[index].chooseTotalMoney;
    }
    that.data.totalPriceYuan = totalPrice;
    // console.log('totalPriceYuan',that.data.totalPriceYuan);
    that.setData({
      wyfList: that.data.wyfList,
      totalPrice: totalPrice * 100,
    })
  },
  /**
   * 
   * @param {所有费用全部选中绑定} e 
   */
  chooseFeeBandAll(e){
    console.log('chooseFeeBand',e)
    var that = this;
    var template = e.currentTarget.dataset.item;
    //点击的是哪个费用类型
    var feeIndex = e.currentTarget.dataset.feeIndex;
    //第几项  哪个月份的
    var tempIndex = e.currentTarget.dataset.index;
    var yearNum =  parseInt(that.data.wyfList[feeIndex].templates[tempIndex].yearNum);
    var monthNum =  parseInt(that.data.wyfList[feeIndex].templates[tempIndex].monthNum);
    
    // 遍历所有的费用类型
    for(var wyfIndex in this.data.wyfList){
      var templates = this.data.wyfList[wyfIndex].templates;
      var feeType = this.data.wyfList[wyfIndex].feeType;
      var chooseFee = { 
        feeType:feeType,
        beginMonth:'',
        endMonth: yearNum + '-' + monthNum,
        chooseList:[]
      }
      // console.log('chooseFeeBand ===', chooseFee)
      that.data.wyfList[wyfIndex].chooseTotalMoney = 0;
  
      // 初始化处理 去掉选中状态
      for(var index in templates){
        var template = templates[index];
        template.isCheck = 0;
        that.data.wyfList[wyfIndex].templates[index] = template;
      }
      templates = that.data.wyfList[wyfIndex].templates;
      // console.log('templates',templates)
      
      //判断当前选中的月份，选中当前月份之前的所有未交费的类型
      var chooseTotalMoney = 0;
      var firstIndex = 0;
      for(var index in templates){
        var template = templates[index];
        var tempYear = parseInt(template.yearNum);
        var tempMonth =  parseInt(template.monthNum);
        console.log('tempYear = ',tempYear)
        console.log('yearNum = ',yearNum)
        //&& tempMonth <= monthNum
        // if((tempYear < yearNum) || (tempYear == yearNum && tempMonth <= monthNum)) {
          console.log('tempYear--yearNum',tempYear,yearNum)
          console.log('tempMonth--monthNum',tempMonth,monthNum)
          if(template.state == 0){
              if(firstIndex == 0){
                chooseFee.beginMonth = template.yearNum +'-' + template.monthNum;
              }
              chooseFee.endMonth = tempYear +'-' + tempMonth;
  
              template.isCheck = 1;
              chooseTotalMoney += template.totalPrice;
      
              if(feeType == 'wyf'){
                if(template.rubbish!=undefined){
                  chooseTotalMoney += template.rubbish.totalPrice;
                  if(template.rubbish.youhuiPrice > 0){
                    chooseTotalMoney -= template.rubbish.youhuiPrice;
                  }
                }
              } else if(feeType == 'water'){
                if(template.jiaya != undefined){
                  chooseTotalMoney += template.jiaya.totalPrice;
                  if(template.jiaya.youhuiPrice > 0){
                    chooseTotalMoney -= template.jiaya.youhuiPrice;
                  }
                }
              }
              
              if(template.youhuiPrice > 0){
                chooseTotalMoney -= template.youhuiPrice;
              }
  
              chooseFee.chooseList.push(template.id);
              firstIndex++;
          }
        // }
        that.data.wyfList[wyfIndex].templates[index] = template;
      }  
      that.data.wyfList[wyfIndex].chooseList = chooseFee;
      that.data.wyfList[wyfIndex].chooseTotalMoney = chooseTotalMoney;
    }

    // console.log('that.data.wyfList',that.data.wyfList);

    var totalPrice = 0;
    for(var index in that.data.wyfList){
        totalPrice += that.data.wyfList[index].chooseTotalMoney;
    }
    that.data.totalPriceYuan = totalPrice;
    // console.log('totalPriceYuan',that.data.totalPriceYuan);
    that.setData({
      wyfList: that.data.wyfList,
      totalPrice: totalPrice * 100,
    })
  },
  initActiveName(templates){
    for(var index in templates){
      this.data.activeNames.push( (parseInt(index+'') + 1) + '');
    }
    console.log('this.data.activeNames',this.data.activeNames)
    this.setData({
      activeNames: this.data.activeNames
    })
  },
  onChangeCollapse(e){
    this.setData({
      activeNames: e.detail,
    });
  },
  onChangeDatePicker(e){
    var type = e.currentTarget.dataset.type;
    this.setData({
      isShowPopup: !this.data.isShowPopup,
    })
    
    if(type == 'begin'){
      this.data.dateType = 0;
      var arrDate = this.data.beginMonth.split("-");
      var time = new Date(arrDate[0],parseInt(arrDate[1]) - 1,1).getTime();
      var date = new Date(arrDate[0],parseInt(arrDate[1]) - 1,1);

      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      this.data.beginMonth = year + ' -' + (month >= 10 ? month :'0' + month);
      this.setData({
        currentDate: time
      })

    }else{
      this.data.dateType = 1;
      var arrDate = this.data.endMonth.split("-");
      var time = new Date(arrDate[0],parseInt(arrDate[1]) - 1,1).getTime();
      var date = new Date(arrDate[0],parseInt(arrDate[1]) - 1,1);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      this.data.endMonth = year + ' -' + (month >= 10 ? month :'0' + month);
      this.setData({
        currentDate: time
      })
    }
    console.log('this.data.beginMonth',this.data.beginMonth);
    console.log('this.data.endMonth',this.data.endMonth);

    
    // this.onHttpInit();
  },
  onConfirmDate(e){
    console.log('onConfirmDate',e)
    var time = new Date(e.detail);
    var month = time.getMonth() + 1;
    var date = time.getFullYear() + '-' + (month < 10 ? '0' + month : month );
    console.log('date',date)

    if(this.data.dateType == 0){
      this.data.beginMonth = date;
      this.setData({
        beginMonth: date
      })
    } else if(this.data.dateType == 1){
      this.data.endMonth = date;
      this.setData({
        endMonth: date
      })
    }
    this.setData({
      isLoading: true,
      isShowPopup:!this.data.isShowPopup,
      wyfList:[],
    })
    
    this.onHttpInit();
  },
  onCancelDate(e){
    console.log('onCancelDate',e)
    this.setData({
      isShowPopup:!this.data.isShowPopup
    })
  },
  onChangeCheckbox(e){
    console.log('onChangeCheckbox',e);
    this.data.checkIds = e.detail;
    this.setData({
      checkIds: e.detail
    })
    var totalPrice = 0;
    var state = true;
    var totalCount = 0;
    var chooseCount = 0;
    var wyfList = this.data.wyfList;
    //修改状态
    for(var mIndex in wyfList){
      var wyf = wyfList[mIndex];
    //   console.log('wyf',wyf)
      var count = 0;//当前月份选中的项数量

      for(var index in wyf.templates){
        var template = wyf.templates[index];
        // console.log('template',template)
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          var tId = template.id;
          for(var sIndex in this.data.checkIds){
            if(tId == this.data.checkIds[sIndex]){
              wyf.templates[index].isCheck = 1;
              totalPrice += template.totalPrice;
              
              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    totalPrice += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    totalPrice += template.jiaya.totalPrice;
                  }
                }
              }

              count ++;
              chooseCount++;
              break;
            }
          }
        }
      }
      wyf.chooseCount = count;
      this.data.wyfList[mIndex] = wyf;
    }
    if(totalCount == chooseCount){
      state = true;
    }else{
      state = false;
    }
    this.data.checkAll = state;

    this.data.wyfList = wyfList;

    this.data.totalPriceYuan = totalPrice;
    this.setData({
      chooseTotalCount:chooseCount,
      checkAll: state,
      totalPrice: totalPrice * 100,
      wyfList:this.data.wyfList,
    })

  },
  onChangeCheckboxNew(e){
    console.log('onChangeCheckbox',e);
    this.data.checkIds = e.detail;

    var lastId = this.data.checkIds[this.data.checkIds.length - 1];
    console.log('lastId',lastId);

    var totalPrice = 0;
    var state = true;
    var totalCount = 0;
    var chooseCount = 0;
    var wyfList = this.data.wyfList;

    //修改状态
    for(var mIndex in wyfList){
      var wyf = wyfList[mIndex];
    //   console.log('wyf',wyf)
      var count = 0;//当前月份选中的项数量

      for(var index in wyf.templates){
        var template = wyf.templates[index];
        // console.log('template',template)
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          var tId = template.id;
          for(var sIndex in this.data.checkIds){
            if(tId == this.data.checkIds[sIndex]){
              wyf.templates[index].isCheck = 1;
              totalPrice += template.totalPrice;
              
              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    totalPrice += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    totalPrice += template.jiaya.totalPrice;
                  }
                }
              }

              count ++;
              chooseCount++;
              break;
            }
          }
        }
      }
      wyf.chooseCount = count;
      this.data.wyfList[mIndex] = wyf;
    }
    if(totalCount == chooseCount){
      state = true;
    }else{
      state = false;
    }
    this.data.checkAll = state;

    this.data.wyfList = wyfList;

    this.data.totalPriceYuan = totalPrice;
    this.setData({
      chooseTotalCount:chooseCount,
      checkAll: state,
      totalPrice: totalPrice * 100,
      wyfList:this.data.wyfList,
      checkIds: e.detail
    })

  },
  /**
   *  全选
   */
  onChangeCheckAll(e){
    console.log('onChangeCheckAll',e)
    var state = e.detail;

    var totalPrice = 0;
    var chooseIds = [];
    var totalCount = 0;
    var chooseCount = 0;
    var checkIds = [];
    var wyfList = this.data.wyfList;
    //修改状态
    for(var mIndex in wyfList){
      var wyf =  wyfList[mIndex];
      console.log('wyf',wyf)
      var count = 0;//当前月份选中的项数量

      for(var index in wyf.templates){
        var template = wyf.templates[index];
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          if(state){
              wyf.templates[index].isCheck = 1;
              totalPrice += template.totalPrice;
              
              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    totalPrice += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    totalPrice += template.jiaya.totalPrice;
                  }
                }
              }

              count ++;
              chooseCount++;
              checkIds.push(template.id+'');
          }else{
              wyf.templates[index].isCheck = 0;
          }
        }
      }
      wyf.chooseCount = count;
      wyfList[mIndex] = wyf;
      if(state){
        var name = parseInt(mIndex+'')+ 1
        chooseIds.push(name+'');
      }
    }
    this.data.totalPriceYuan = totalPrice;
    this.data.checkAll = state;
    this.data.wyfList = wyfList;
    console.log("chooseIds",chooseIds)
    console.log("checkIds",checkIds)
    this.data.activeNames = chooseIds;
    this.setData({
      chooseTotalCount: chooseCount,
      checkAll: state,
      checkIds:checkIds,
      totalPrice: totalPrice * 100,
      wyfList: this.data.wyfList,
      activeNames: chooseIds,
    })
  },
  /**
   *  一键选择
   */
  handleOneKey(e){
    var type = e.currentTarget.dataset.type;
    var state = true;
    var chooseIds = [];
    var totalCount = 0;
    // var chooseCount = 0;
    
    var wyfList = this.data.wyfList;
    //修改状态
    for(var mIndex in wyfList){
      var wyf = wyfList[mIndex];
      console.log('wyf',wyf)
      for(var index in wyf.templates){
        var template = wyf.templates[index];
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          if(wyf.templates[index].feeType == type){
            if(wyf.templates[index].isCheck == 0){
              wyf.templates[index].isCheck = 1;
              this.data.totalPriceYuan += template.totalPrice;

              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    this.data.totalPriceYuan += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    this.data.totalPriceYuan += template.jiaya.totalPrice;
                  }
                }
              }

              wyf.chooseCount ++;
              this.data.chooseTotalCount++;
              if(this.data.checkIds.indexOf(template.id+'') < 0){
                this.data.checkIds.push(template.id+'');
              }

            }
          }
        }
      }
      wyfList[mIndex] = wyf;
      if(wyf.chooseCount > 0){
        var name = parseInt(mIndex+'')+ 1
        if(this.data.activeNames.indexOf(name) < 0){
          this.data.activeNames.push(name+'');
        }
      }
    }
    this.data.wyfList = wyfList;
    
    if(totalCount == this.data.chooseTotalCount){
      state = true;
    }else{
      state = false;
    }
    this.data.checkAll = state;

    console.log(" this.data.activeNames", this.data.activeNames)
    // console.log("checkIds",checkIds)

    this.setData({
      chooseTotalCount: this.data.chooseTotalCount,
      checkAll: state,
      checkIds:this.data.checkIds,
      totalPrice: this.data.totalPriceYuan * 100,
      wyfList: this.data.wyfList,
      activeNames: this.data.activeNames,
    })

  },
  submitOrder:function(e){
    var that = this;
    if(this.data.submitting == true){
      Toast("已提交，请稍后...");
      return;
    }
    var nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      Toast("您点的太快了，请稍后...");
      return;
    }
    this.data.tapTime = nowTime;


    // if(this.data.checkIds.length == 0){
    //   Toast.fail('请选择缴费项目');
    //   return;
    // }

    //将选中的id 的数组 转换成 拼接字符串  逗号分隔
    // var ids = this.data.checkIds.join();

    if(this.data.totalPrice <= 0){
      Toast.fail('请选择缴费项目');
      return;
    }

    var chooseCount = 0;
    var chooseFee = [];
    for(var index in that.data.wyfList){
        var chooseList = that.data.wyfList[index].chooseList;
        console.log('chooseList',chooseList)
        // console.log('chooseList.length',chooseList.length)
        if(chooseList!= undefined && chooseList.chooseList.length > 0 && chooseList.feeType!=undefined){
          chooseCount += chooseList.length;
          chooseFee.push(chooseList);
        }
    }
    console.log('chooseFee',chooseFee)
    console.log('chooseCount',chooseCount)
    
    if(chooseCount == 0){
        Toast("请选择要支付的费用");
        return;
    }
    this.data.submitting == true;
    that.setData({ submitting : true})
    fetchApi.httpPost('/property/addOrderByTemplate1', {
      room_id: that.data.room_id,
      begin_month: that.data.beginMonth,
      end_month: that.data.endMonth,
      chooseFee: chooseFee,
    }, function (res) {
      console.log('res', res)
      if (res.data.code == 0) {
        var order_id = res.data.data;
        console.log('order_id',order_id);
        wx.navigateTo({
          url: '/pagesProperty/pages/propertySettle/index?is_reg=0&order_id='+order_id,
        })
      } else if(res.data.code == 2){
        const message = '您有未支付的订单，去支付？'
        Dialog.confirm({
          title: '温馨提示',
          message
        }).then(() => {          
          wx.navigateTo({
            url: '/pagesProperty/pages/propertyList/index',
          })
        })
        .catch(() => {
          Dialog.close();
        });
      }
      else {
        Toast.fail(res.data.msg);
      }
      that.data.submitting = false;
      that.setData({
        submitting:false
      })
    },function(){})
  }, 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})